#define _CRT_SECURE_NO_WARNINGS

#include "Stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	if(ps->top==ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4:ps->capacity*4 ;
		STDataType* tmp = (STDataType*)realloc(ps->a,sizeof(STDataType) * newCapacity);
		ps->a = tmp;
		ps->capacity = newCapacity;
	}
	ps->a[ps->top] = data;
	ps->top++;
}
void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	--ps->top;
}
int StackEmpty(Stack* ps)
{
	assert(ps);
	return (ps->top == 0);
}
void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
STDataType StackTop(Stack* ps)
{
	assert(ps);
	return ps->a[ps->top - 1];
}